<!DOCTYPE html>
<html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml" xmlns:v-model="http://www.w3.org/1999/xhtml"
      xmlns:v-on="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../../js/vue/vue.js"></script>

    <script src="../../js/jquery/jquery.js"></script>

    <script src="../../js/jquery-form/jquery.form.js"></script>
</head>
<body>
<div id="app">

    <form id="formId">

        <input type="hidden" name="id" v-model="emp.id">

        名称：<input type="text" name="name" v-model="emp.name"><br>

        年龄：<input type="text" name="age" v-model="emp.age"><br>

        <input type="button" id="btn" @click="submitForm()" value="提交"></input>

    </form>

</div>
<script>

    //获取url上的请求参数
    function getParams() {
        //获取问号及问号后面的内容
        var url = window.location.search;
        var params = new Object();
        if (url.indexOf("?") != -1) {
            //截取问号后面的内容,再使用&分割多个属性
            var arr = url.substr(1).split("&");
            for (var i = 0; i < arr.length; i++) {
                //使用=分割为keyvalue
                var keyValue = arr[i].split("=");
                params[keyValue[0]] = keyValue[1];
            }
        }
        return params;
    }

    var app = new Vue({
        el:'#app',
        data:{
            emp:{}
        },
        methods:{
            submitForm:function () {
                $("#formId").ajaxSubmit({
                    url:"http://localhost:8080/emps/saveOrUpdate",
                    type:"post",
                    success:function (data) {
                        if(data.success){
                            window.location.href="index.html"
                        }
                    }
                })
            }
        },
        mounted:function () {
           var empId = getParams().id;
            if(empId){
                var _this=this;
                $.get("http://localhost:8080/emps/detail",{id:empId},function (data) {
                    console.log(data);
                    _this.emp=data
                })
            }
        }
    })
</script>
</body>
</html>